package com.xianbeiying.app.ckgl.model;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.bean.JsonBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.http.HttpPostManger;
import com.xianbeiying.app.ckgl.util.DialogUtile;
import com.xianbeiying.app.ckgl.util.LogUtils;
import com.xianbeiying.app.ckgl.util.ToastUtil;

import cz.msebera.android.httpclient.Header;

/**
 * 图片上传
 */
public class ImageModel extends BaseModel{
    private AsyncHttpClient client;
    private Context context;
    public ImageModel(Context context) {
        this.context = context;
        client = new AsyncHttpClient();
    }
   /**
    * 图片上传
    * */
    public void ImageLogin(String USERNAME, int USERID, String  USERURL, boolean isDiaylog) {
        if (isDiaylog) {
            DialogUtile.showLoading(context);
        }
        String url=JsonBean.changeImageString(USERNAME,USERID,USERURL);
        LogUtils.e("头像上传","http://"+MyApplication.IP+UrlBean.USERMANAGER+"update_user"+url+"====");
        HttpPostManger.post(context,"http://"+MyApplication.IP+UrlBean.USERMANAGER+"update_user",url, new AsyncHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                //关闭加载
                DialogUtile.closeDialog();
//                Log.e("返回值",new String(responseBody));
                //函数返回值
                ImageModel.this.onCallBackData("图片",new String(responseBody));
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                //关闭加载
                DialogUtile.closeDialog();
                LogUtils.e("返回值", "----------------"+statusCode);
                if(statusCode==0){
                    ToastUtil.showInfo(context,"无法连接平台");
                }else if(statusCode==302){
                    MyApplication.isLogin=false;
//                    ToastUtil.showInfo(context,"请先登录");
                }else{
                    ToastUtil.showInfo(context,"无法连接平台");
                }
            }
        });
    }

    /**
     * 历史图片
     * */
    public void ImageHistoLogins(String date,int greesnId, boolean isDiaylog) {
        if (isDiaylog) {
            DialogUtile.showLoading(context);
        }
        String url=JsonBean.historyImage(date,greesnId);
        LogUtils.e("历史图片","http://"+MyApplication.IP+UrlBean.HistImage+url+"====");
        HttpPostManger.post(context,"http://"+MyApplication.IP+UrlBean.HistImage,url, new AsyncHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                //关闭加载
                DialogUtile.closeDialog();
                Log.e("返回值",new String(responseBody));
                //函数返回值
                ImageModel.this.onCallBackData("历史图片",new String(responseBody));
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                //关闭加载
                DialogUtile.closeDialog();
                if(statusCode==0){
                    ToastUtil.showInfo(context,"无法连接平台");
                }else if(statusCode==302){
                    MyApplication.isLogin=false;
//                    ToastUtil.showInfo(context,"请先登录");
                }else{
                    ToastUtil.showInfo(context,"无法连接平台");
                }
            }
        });
    }

}
